package com.juzhi.dvrapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.view.View;
import android.widget.TextView;

import com.juzhi.dvrapp.R;
import com.juzhi.dvrapp.bean.Device;
import com.juzhi.dvrapp.manager.DeviceManager;
import com.juzhi.dvrapp.utils.Config;

import butterknife.BindView;
import butterknife.OnClick;

public class DeviceInfoActivity extends BaseAcitivity {


    @BindView(R.id.activity_device_info_name)
    TextView mDeviceName;
    @BindView(R.id.activity_device_info_ssid)
    TextView mDeviceSsid;
    @BindView(R.id.activity_device_info_state)
    TextView mDeviceState;
    private DeviceManager deviceManager;
    private Device device;

    @Override
    public int getLayoutId() {
        return R.layout.activity_device_info;
    }

    @Override
    protected void initView() {
        initHeaderView();
        hideFunction();

        String ssid = getIntent().getStringExtra(Config.SSID);
        deviceManager = DeviceManager.getDeviceManager(this);
        device = deviceManager.getDevice(ssid);

        setTitleText(device.getDeviceModel().getDeviceName());
        mDeviceName.setText(device.getDeviceModel().getDeviceName());
        mDeviceSsid.setText(device.getDeviceModel().getSsid());
        if (device.getStatus() == Config.DEVICE_RESULT_SUCCESS){
            mDeviceState.setText(getResources().getString(R.string.device_online));
        }
    }

    @Override
    protected void back() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }


    @OnClick({R.id.avtivity_device_info_item, R.id.activity_device_info_message_item,
            R.id.activity_device_info_play_item, R.id.activity_device_info_video_item,
            R.id.activity_device_info_download_item, R.id.activity_device_info_talkback_item,
            R.id.activity_device_info_track_item, R.id.activity_device_info_fence_item,
            R.id.activity_device_info_remotesetting_item})
    public void onViewClicked(View view) {
        Intent intent = new Intent();
        intent.putExtra(Config.SSID, device.getDeviceModel().getSsid());
        switch (view.getId()) {
            case R.id.avtivity_device_info_item:
                intent.setClass(DeviceInfoActivity.this, DeviceUserActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_device_info_message_item:
                intent.setClass(DeviceInfoActivity.this,DeviceMessageActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_device_info_play_item:
                goToPlayEmptyControlActivity(DeviceInfoActivity.this,view);
                break;
            case R.id.activity_device_info_video_item:
                break;
            case R.id.activity_device_info_download_item:
                break;
            case R.id.activity_device_info_talkback_item:
                intent.setClass(DeviceInfoActivity.this, DeviceTalkActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_device_info_track_item:
                intent.setClass(DeviceInfoActivity.this, DeviceTrackActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_device_info_fence_item:
                intent.setClass(DeviceInfoActivity.this, DeviceFenceActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_device_info_remotesetting_item:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    /**
     * 跳转到无UI视频播放
     *
     * @param activity
     * @param view
     */
    public void goToPlayEmptyControlActivity(Activity activity, View view) {
        Intent intent = new Intent(activity, DevicePlayActivity.class);
        intent.putExtra(DevicePlayActivity.TRANSITION, true);
        intent.putExtra(Config.SSID, device.getDeviceModel().getSsid());
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            Pair pair = new Pair<>(view, DevicePlayActivity.IMG_TRANSITION);
            ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
                    activity, pair);
            ActivityCompat.startActivity(activity, intent, activityOptions.toBundle());
        } else {
            activity.startActivity(intent);
            activity.overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
        }
    }
}
